﻿using System;
//
using System.Runtime.InteropServices;
//
// Mp3Cleaner, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
//
namespace FlashWindow
{
    class FlashWindow
    {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

        /// <summary>
        /// CAPTION: blink caption, TRAY: blink tray, ALL: blink caption + tray, UNTILFG: blink until the window comes to the foreground:
        /// </summary>
        public enum FLAGS:uint { CAPTION = 1, TRAY = 2, ALL = 3, UNTILFG = 4 };

        [StructLayout(LayoutKind.Sequential)]
        private struct FLASHWINFO
        {
            /// <summary>
            /// The size of the structure in bytes.
            /// </summary>
            public uint cbSize;
            /// <summary>
            /// A Handle to the Window to be Flashed. The window can be either opened or minimized.
            /// </summary>
            public IntPtr hwnd;
            /// <summary>
            /// The Flash Status.
            /// </summary>
            public FLAGS dwFlags;
            /// <summary>
            /// The number of times to Flash the window.
            /// </summary>
            public uint uCount;
            /// <summary>
            /// The rate at which the Window is to be flashed, in milliseconds. If Zero, the function uses the default cursor blink rate.
            /// </summary>
            public uint dwFlashRate;
        }
        //
        public static bool Blink(IntPtr handle, int count)
        {
            return Blink(handle, FLAGS.ALL, count, 0);
        }
        public static bool Blink(IntPtr handle, FLAGS flags, int count, int timeout)
        {
            FLASHWINFO fi = new FLASHWINFO();
            fi.cbSize = Convert.ToUInt32(Marshal.SizeOf(fi));
            fi.hwnd = handle;
            fi.dwFlags = flags;
            fi.uCount = (uint)count;
            fi.dwFlashRate = (uint)timeout;
            //
            return FlashWindowEx(ref fi);
        } 
    }//class
}//namespace
